package net

import (
	"dgo/MyProj/PrjHttpUtils"
	"dgo/MyProj/PrjHttpWs"
	"dgo/goutils/utils"
	"fmt"
	"gitee.com/ymofen/supervalue"
)

/*
*

	ws 发送插件
	不推荐使用
*/
type ProjWsCltPlug struct {
	PlgBase
	conf         *wrapper.SuperValue
	plugId       string
	wsclt        *PrjHttpWs.WsHttpClt
	monitor_send *utils.MonitorSpeedSizeRec
	monitor_recv *utils.MonitorSpeedSizeRec

	outputEx *PlgBaseInnerEx
}

func (this *ProjWsCltPlug) OnHttpWsRecvTopic(sender *PrjHttpWs.WsHttpClt, topic string, data interface{}) {
	/**
	  数据分发
	*/
	this.outputEx.DispatchData(topic, sender, data, this)
	//sc := this.out_sc;
	//if sc != nil{
	//	sc.DispatchData(topic, sender, data, this)
	//}

	return
}

func (this *ProjWsCltPlug) SendKVs(topic string, kv ...interface{}) error {
	if this.wsclt != nil {
		l := len(kv)
		if l == 1 {
			return this.wsclt.PublishMessage(topic, kv[0])
		}

		json := wrapper.NewSVObject()
		json.SetKeyValues(kv...)
		return this.wsclt.PublishMessage(topic, json)
	} else {
		return fmt.Errorf("WS连接尚未进行创建")
	}
}

func (this *ProjWsCltPlug) OnHttpStatus(rec *PlgHttpRec) (rval interface{}, errcode int, err error) {
	json := wrapper.NewSVObject()
	json.SetKeyValues("send-size", this.monitor_send.SizeInfo(), "recv-size", this.monitor_recv.SizeInfo(),
		"conn-n", this.wsclt.GetConnNum(),
		"connected", this.wsclt != nil && this.wsclt.IsConnected(), "lastdis", this.wsclt.LastDisreason())
	PrjHttpUtils.JSON2TableRec(rec.RespW, "", json.AsJSONString(false))
	return
}

/*
**

	{
	  "id":"core_kpl_wsclt",
	  "type":"http.wsclt",
	  "conf":{
		"url":"http://xxx.xx.xx:xxx/$appuuid$",
	    "topic-add":"",
	    "output":{
	       "id":"ex",
	       "topic":"BJ05"
	     }
	   }
	}
*/
func (this *ProjWsCltPlug) ReloadConf(conf *wrapper.SuperValue) error {
	if this.outputEx == nil {
		this.outputEx = PlgNewInnerEx()
	}

	if this.monitor_send == nil {
		this.monitor_send = utils.NewMonitorSpeedSizeRec()
		this.monitor_recv = utils.NewMonitorSpeedSizeRec()
		utils.Monitor.AddMonitorRec(this.monitor_send, this.monitor_send)
		utils.Monitor.AddMonitorRec(this.monitor_recv, this.monitor_recv)
	}

	wsurl := conf.StringByPath("url", "")

	if len(wsurl) > 0 && (this.conf == nil || wsurl != this.conf.StringByPath("url", "")) {
		presub := ""
		if this.wsclt != nil {
			presub = this.wsclt.GetSubscribleTopics()
			utils.GoFunc2(this.wsclt.Stop)
			this.wsclt = nil

		}
		wsclt := PrjHttpWs.NewWsHttpClt(wsurl, "")
		wsclt.ConfigOnRecvTopic(this.OnHttpWsRecvTopic)
		wsclt.ConfigMonitor(this.monitor_send, this.monitor_recv)
		utils.GoFunc2(wsclt.Start)
		if len(presub) > 0 {
			wsclt.SubScribleTopic(presub)
		}
		this.wsclt = wsclt
	}

	topic_add := conf.StringByPath("topic-add", "")
	if len(topic_add) > 0 {
		if this.wsclt == nil {
			return fmt.Errorf("请先指定URL进行创建WS连接")
		}
		this.wsclt.SubScribleTopic(topic_add)
	}

	this.conf = conf
	this.outputEx.ReloadConf(conf.ValueByName("output"))

	return nil
}

func (this *ProjWsCltPlug) OnObjectNotify(sender interface{}, notifytype int, data interface{}) (rval interface{}, err error) {
	if notifytype == utils.NOTIFY_TYPE_SETCONF {
		if conf, ok := data.(*wrapper.SuperValue); ok {
			err = this.ReloadConf(conf)
		} else {
			err = fmt.Errorf("非法的JSON配置")
		}
	} else if notifytype == utils.NOTIFY_TYPE_RELEASE {
		if this.outputEx != nil {
			this.outputEx.Release()
		}

	}
	return
}

func CreateProjWsCltPlugFunc(args ...interface{}) (rval interface{}, err error) {
	obj := &ProjWsCltPlug{}
	if len(args) > 0 {
		if str, ok := args[0].(string); ok {
			obj.plugId = str
		}
	}
	rval = obj
	return
}
